package v1

import (
	"fmt"
	"gin-vue-admin/global/response"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
	resp "gin-vue-admin/model/response"
	"gin-vue-admin/service"

	"gin-vue-admin/global"
	"github.com/gin-gonic/gin"
)

// @Tags P201Delivers
// @Summary 创建P201Delivers
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.P201Delivers true "创建P201Delivers"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /p201Delivers/createP201Delivers [post]
func CreateP201Delivers(c *gin.Context) {
	var p201Delivers model.P201Delivers

	//body, _ := ioutil.ReadAll(c.Request.Body)
	//fmt.Println("---body/--- \r\n " + string(body))
	global.GVA_LOG.Info("----------- 创建")
	err := c.ShouldBindJSON(&p201Delivers)
	if err != nil {
		response.FailWithMessage(fmt.Sprintf("数据绑定失败，%v", err), c)
		return
	}
	global.GVA_LOG.Info("----- obj ----------")
	// global.GVA_LOG.Info(p201Delivers)

	// p201Delivers.CustCreateTime = time.Now()
	// p201Delivers.DeliverTime = time.Now()

	err = service.CreateP201Delivers(p201Delivers)
	if err != nil {
		response.FailWithMessage(fmt.Sprintf("创建失败，%v", err), c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// @Tags P201Delivers
// @Summary 删除P201Delivers
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.P201Delivers true "删除P201Delivers"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /p201Delivers/deleteP201Delivers [delete]
func DeleteP201Delivers(c *gin.Context) {
	var p201Delivers model.P201Delivers
	_ = c.ShouldBindJSON(&p201Delivers)
	err := service.DeleteP201Delivers(p201Delivers)
	if err != nil {
		response.FailWithMessage(fmt.Sprintf("删除失败，%v", err), c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// @Tags P201Delivers
// @Summary 批量删除P201Delivers
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除P201Delivers"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /p201Delivers/deleteP201DeliversByIds [delete]
func DeleteP201DeliversByIds(c *gin.Context) {
	var IDS request.IdsReq
	_ = c.ShouldBindJSON(&IDS)
	err := service.DeleteP201DeliversByIds(IDS)
	if err != nil {
		response.FailWithMessage(fmt.Sprintf("删除失败，%v", err), c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// @Tags P201Delivers
// @Summary 更新P201Delivers
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.P201Delivers true "更新P201Delivers"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /p201Delivers/updateP201Delivers [put]
func UpdateP201Delivers(c *gin.Context) {
	var p201Delivers model.P201Delivers
	global.GVA_LOG.Info("------------ 更新 ")
	err := c.ShouldBindJSON(&p201Delivers)
	if err != nil {
		response.FailWithMessage(fmt.Sprintf("数据绑定失败，%v", err), c)
		return
	}
	global.GVA_LOG.Info("------------ 更新后 ")
	fmt.Println(p201Delivers)
	fmt.Println(p201Delivers.DeliverTime.String())

	err = service.UpdateP201Delivers(&p201Delivers)
	if err != nil {
		response.FailWithMessage(fmt.Sprintf("更新失败，%v", err), c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// @Tags P201Delivers
// @Summary 用id查询P201Delivers
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.P201Delivers true "用id查询P201Delivers"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /p201Delivers/findP201Delivers [get]
func FindP201Delivers(c *gin.Context) {
	var p201Delivers model.P201Delivers
	_ = c.ShouldBindQuery(&p201Delivers)
	err, rep201Delivers := service.GetP201Delivers(p201Delivers.ID)
	if err != nil {
		response.FailWithMessage(fmt.Sprintf("查询失败，%v", err), c)
	} else {
		response.OkWithData(gin.H{"rep201Delivers": rep201Delivers}, c)
	}
}

// @Tags P201Delivers
// @Summary 分页获取P201Delivers列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.P201DeliversSearch true "分页获取P201Delivers列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /p201Delivers/getP201DeliversList [get]
func GetP201DeliversList(c *gin.Context) {
	var pageInfo request.P201DeliversSearch
	_ = c.ShouldBindQuery(&pageInfo)
	err, list, total := service.GetP201DeliversInfoList(pageInfo)
	if err != nil {
		response.FailWithMessage(fmt.Sprintf("获取数据失败，%v", err), c)
	} else {
		response.OkWithData(resp.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, c)
	}
}
